package main;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class Stopper implements Runnable
{
     public boolean working = true;
     private JFrame frame;
     private int time_blue_screen;

     Robot robi;
     
     public Stopper(JFrame frame)
     {
          this.frame = frame;
          frame.addKeyListener(listen);
          try {
			robi = new Robot();
		} catch (AWTException e) {}
     }

     public void stop()
     {
          working = false;
     }

     public static Stopper create(JFrame frame)
     {
         Stopper stopper = new Stopper(frame);
         new Thread(stopper, "Alt-Tab Stopper").start();
         return stopper;
     }

     public void run()
     {
         try
         {
        	 time_blue_screen = (Test_rs.blue_screen_time/10); 
        	 int i = 1;
             Robot robot = new Robot();
             while (working && i<time_blue_screen)
             {
                  //robot.keyRelease(KeyEvent.VK_ALT);
                  robot.keyRelease(KeyEvent.VK_TAB);
                  robot.keyRelease(KeyEvent.VK_CONTROL);
                  //robot.keyRelease(KeyEvent.VK_WINDOWS);
                  frame.requestFocus();
                  i +=1;
                  try { Thread.sleep(10); } catch(Exception e) {}
             }
         } catch (Exception e) { e.printStackTrace(); System.exit(-1); }
     }
     
     boolean d1 = false;
     boolean d2 = false;
     boolean d3 = false;
     
     KeyListener listen = new KeyListener() {
		
		@Override
		public void keyTyped(KeyEvent e) {	}
		
		@Override
		public void keyReleased(KeyEvent e) {
			if (e.getKeyCode()==82){
				d1 = false;
			} else if (e.getKeyCode()==77){
				d2 = false;
			} else if (e.getKeyCode()==79){
				d3 = false;
			}
		}
		
		@Override
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode()==82){
				d1 = true;
			} else if (e.getKeyCode()==77){
				d2 = true;
			} else if (e.getKeyCode()==79){
				d3 = true;
			} else if (e.getKeyCode()==KeyEvent.VK_WINDOWS){					
				robi.keyRelease(KeyEvent.VK_WINDOWS);
				robi.keyPress(KeyEvent.VK_WINDOWS);
				robi.keyRelease(KeyEvent.VK_WINDOWS);
			} else if (e.getKeyCode()==KeyEvent.VK_ALT){					
				robi.keyRelease(KeyEvent.VK_ALT);
				robi.keyPress(KeyEvent.VK_ALT);
				robi.keyRelease(KeyEvent.VK_ALT);
			}
			if (d1 && d2 && d3){
				working = false;
				System.exit(0);
			}
		}
		
	};
     
     
}